<?php  
/* 
Template Name: Page New
*/
?><?php get_header();?>
<div class="cy-content">
	<?php get_sidebar();?>
    <div class="cy-content-center">
    <?php locate_template('block/block-search.php', true); ?>
    <style type="text/css" media="screen">
			.tab_new { padding: 0; clear: both; border-bottom: 2px solid #cccccc; overflow: hidden; margin-bottom: 20px; }
			.tab_new li { display: inline; }
			.tab_new li a { padding: 5px 10px 5px 0; float:left;text-decoration: none; color: #ababab; font-weight: bold; font-size: 16px; text-transform: uppercase;}
			.tab_new li.active a { color: #c7b43b }
			.content-news { float: left; clear: both; padding: 10px 20px 20px; }
		</style>
 	  <ul id="tab_new" class="tab_new">
			<li class="active" ><a href="#1">Tin mới nhất</a></li>
			<li><a href="#2">Xem nhiều nhất</a></li>
		</ul>
		<div id="1" class="content-news">        
            <table width="100%">
              <tr class="tr-title">
                <td width="27">STT</td>
                <td width="322">TIN TỨC MỚI NHẤT</td>
                <td width="102">NGƯỜI ĐĂNG</td>
                <td width="101">NGÀY ĐĂNG</td>
                <td width="83">LƯỢT XEM</td>
              </tr>
             <?php
                 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                 $i=1;
                 $loop = new WP_Query(array('post_type'=>'news','posts_per_page'=>10,'order'=>'DESC', 'paged' => $paged));
              ?>
               <?php while ( $loop->have_posts() ) : $loop->the_post(); //bat dau vong lap ?>
              <tr>
                <td><?php echo $i;?></td>
                <td><a href="<?php the_permalink();?>"><?php the_title(); echo $post->ID;?></a></td>
                <td><?php
                global $userdata;
                get_currentuserinfo();
                echo $userdata->display_name;?></td>
                <td><?php echo get_the_date('d/m/Y'); ?></td>
                <td><?php 
                if(get('luot_truy_cap')==""){
                    echo '0';
                }else{
                echo get('luot_truy_cap');
                }
                ?>
                </td>
              </tr>
               <?php 
               $i++;
               endwhile; //ket thuc vong lap ?>
               <?php wp_reset_query();?>
            </table>
		</div><!--content-news-->
		<div id="2" class="content-news">
         <?php 
        $items = mysql_num_rows(mysql_query("SELECT wposts. * , wpostmeta . *
				FROM wp_posts wposts, wp_postmeta wpostmeta
				WHERE wposts.ID = wpostmeta.post_id
				AND wpostmeta.meta_key = 'luot_truy_cap'
				AND wposts.post_type = 'news'
				ORDER BY CAST( wpostmeta.meta_value AS UNSIGNED ) DESC"));
        //print_r($items);
       //$items;
        ?>
            <table width="100%">
              <tr class="tr-title">
                <td width="27">STT</td>
                <td width="322">TIN TỨC MỚI NHẤT</td>
                <td width="102">NGƯỜI ĐĂNG</td>
                <td width="101">NGÀY ĐĂNG</td>
                <td width="83">LƯỢT XEM</td>
              </tr>
            <?php 
			$j=1;
            $page=get_query_var('paged');
              if($page==""){
              $page=1;
            }
            $limit=10;
            $start=$page*$limit-$limit;
            
			  $results = $wpdb->get_results("SELECT wposts. * , wpostmeta . *
				FROM wp_posts wposts, wp_postmeta wpostmeta
				WHERE wposts.ID = wpostmeta.post_id
				AND wpostmeta.meta_key = 'luot_truy_cap'
				AND wposts.post_type = 'news'
				ORDER BY CAST( wpostmeta.meta_value AS UNSIGNED ) DESC
                LIMIT $start,$limit
                ");
			foreach ($results as $result) {
			?>
              <tr>
                <td><?php echo $j;?></td>
                <td><a href="<?php echo bloginfo('url');?>/?<?php echo $result->post_type;?>=<?php echo $result->post_name;?>"><?php echo $result->post_title;?></a></td>
                <td>
                <?php $user_info = get_userdata($result->post_author);
      			echo  $user_info->display_name;
				?>
                </td>  
                <td><?php $tungay=$result->post_date;
                	$tungay_arr = explode("-",$tungay); // array(17,11,1990)
			$tn="1970-01-01"; //từ ngày : mặc định là 1970-01-01
			if (count($tungay_arr)==3) {
				 $d = $tungay_arr[0]; //17
				 $m = $tungay_arr[1]; //11
				 $y = substr($tungay_arr[2],0,2); //1990
                echo $y.'/'.$m.'/'.$d;
			}
               
                
                ?></td>
                <td><?php echo $result->meta_value;?></td>
              </tr>
 			<?php 
			$j++;
			} ?>
  
        </table>
            <center>
            <div class="cy-pagination">
            <?php // OK, custom tax's dont work nice with query_posts (i think) but they DEF dont work nice with next_posts(), previous_posts() and  posts_nav_link()
            // SO, got the custom page nav from here:
            // http://wordpress.org/support/topic/adding-pagination-to-a-wp_query-loop?replies=16
	       	$trang=$items/10;
		      $trangthuc=ceil($trang);   
		      if($trangthuc>1){
            if ($paged > 1) { ?>
             <a class="cy-first-btn" href="<?php echo '?page_id=134'.'&paged=1'.'#2-tab'; //next link ?>"></a>            
            <a class="cy-prev-btn" href="<?php echo 'page_id=134'.'paged=' . ($paged -1).'#2-tab'; //prev link ?>"></a>
            <?php
            }
            for($i=1;$i<=$trangthuc;$i++){?>
          	<?php if($i==$paged){?>
            <span><?php echo $i;?></span>
			<?php } else {?>
            <a href="<?php echo '?page_id=134'.'&paged=' . $i.'#2-tab'; ?>" <?php echo ($paged==$i)? 'class="selected"':'';?>><?php echo $i;?></a>
          <?php
			}//end if
          } //end for
          if($paged < $trangthuc){?>
            <a class="cy-next-btn" href="<?php echo '?page_id=134'.'paged=' . ($paged + 1).'#2-tab'; //next link ?>"></a>
              <a class="cy-last-btn" href="<?php echo '?page_id=134'.'paged=' . ($trangthuc).'#2-tab'; //next link ?>"></a>      
          <?php
          }
        }?>
        </div><!--cy-pagination-->
        </center>
		</div> 
    </div><!--cy-content-center-->  
</div><!--cy-content-->
<?php get_footer(); ?>